/**
 * Http扩展方法
 * @author linjianqun
 * @version 2019-02-23
 */

/**
 * 表单提交方法
 * @param { Object } httpInstance
 * @param { String } url
 * @param { Object| String } data
 * @param { Object } config
 * @returns
 */
function postFormData(url, data, config) {
  let formData = '';
  if (data && typeof data === 'object') {
    let value;
    Object.keys(data).forEach((key) => {
      if (formData) {
        formData += '&';
      }
      value = data[key] || '';
      formData += `${key}=${value}`;
    });
  } else if (data && typeof data === 'string') {
    formData = data;
  }
  const postFormConfig = config || {};
  if (!postFormConfig.headers) {
    postFormConfig.headers = {};
  }
  postFormConfig.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
  return this.post(url, formData, postFormConfig);
}

/**
 * 转换为微服务配置参数
 * @param {*} config
 * @returns
 */
function _translateToMicroServerConfig(config) {
  return Object.assign({
    isMicroServer: true
  }, config || {});
}

/**
 * 提供给微服务接口调用的get
 * @param {*} httpInstance
 * @param {*} url
 * @param {*} config
 */
function getForMicroServer(url, config) {
  const newConfig = _translateToMicroServerConfig(config);
  return this.get(url, newConfig);
}

/**
 * 提供给微服务接口调用的post
 * @param {*} httpInstance
 * @param {*} url
 * @param {*} data
 * @param {*} config
 */
function postForMicroServer(url, data, config) {
  const newConfig = _translateToMicroServerConfig(config);
  return this.post(url, data, newConfig);
}

/**
 * 提供给微服务接口调用的postFormData
 * @param {*} httpInstance
 * @param {*} url
 * @param {*} data
 * @param {*} config
 */
function postFormDataForMicroServer(url, data, config) {
  const newConfig = _translateToMicroServerConfig(config);
  return this.postFormData(url, data, newConfig);
}

/**
 * 访问restful接口
 * @param {*} url
 * @param {*} config
 * @returns
 */
function getRestful(url, config) {
  let newUrl = url;
  if (newUrl && config && config.params && typeof config.params === 'object') {
    Object.keys(config.params).forEach((key) => {
      newUrl = newUrl.replace(new RegExp(`{${key}}`, 'gm'), config.params[key]);
    });
    config.params = {};
  }
  return this.get(newUrl, config || {});
}
// 处理
function processParams(url, config) {
  if (url && config && config.params && typeof config.params === 'object') {
    Object.keys(config.params).forEach((key) => {
      url = url.replace(new RegExp('{' + key + '}', 'gm'), config.params[key] + '');
    });
    config.params = {};
  }
  let params = {
    url,
    config
  }
  return params;
}
/**
 * 访问restful接口, demo: api/{pageNo}/{pageSize}
 * @param {*} url
 * @param {*} data
 * @param {*} config
 * @returns
 */
function postRestful(url, data, config) {
  let paramObj = processParams(url, config)
  return this.post(paramObj.url, data, paramObj.config || {});
}
module.exports =  function httpExtendMethod(httpInstance) {
  if (!httpInstance) {
    return;
  }
  httpInstance.postFormData = postFormData;
  httpInstance.getRestful = getRestful;
  httpInstance.postRestful = postRestful;
  httpInstance.getForMicroServer = getForMicroServer;
  httpInstance.postForMicroServer = postForMicroServer;
  httpInstance.postFormDataForMicroServer = postFormDataForMicroServer;
}
